package com.njym.java.security.app.rest;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njym.java.security.app.biz.LiuyanbanBiz;
import com.njym.java.security.app.entity.Liuyanban;
import com.njym.java.security.app.entity.LiuyanbanVo;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.msg.TableResultResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("liuyanban")
public class LiuyanbanController extends BaseController<LiuyanbanBiz,Liuyanban> {

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<LiuyanbanVo> allLiuyan(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int page){
        List<LiuyanbanVo> list = baseBiz.allLiuyan();
        long total = PageInfo.of(list).getTotal();
        int  totalPages= (int)Math.ceil((double)total/limit);
        Map<String, Object> data = new HashMap<>(3);
        data.put("list",list);
        data.put("total",total);
        data.put("totalPages",totalPages);
        return new ObjectRestResponse<LiuyanbanVo>().rel(true).data(data);
    }

    @RequestMapping(value = "/getLiuyan",method = RequestMethod.GET)
    @ResponseBody
    public Object getLiuyan(Integer page, Integer limit){
        Page<Object> result = PageHelper.startPage(page,limit);
        baseBiz.selectLiuyan();
        return new TableResultResponse<>(result.getPages(),result.getResult());
    }


    @RequestMapping(value = "/create",method = RequestMethod.GET)
    @ResponseBody
    public Object create(String userId,String content){
        if (!userId.equals("undefined")) {
            Liuyanban  liuyanban = new Liuyanban();
            liuyanban.setUserId(Integer.parseInt(userId));
            liuyanban.setContent(content);
            liuyanban.setDeleted(0);
            liuyanban.setCreateTime(new Date());
            baseBiz.insert(liuyanban);
            return  ResponseUtil.ok();
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

}